# from flask import Flask
from flask import (Flask, flash, request, redirect, render_template, url_for, session)
app = Flask(__name__)
import requests
from furl import furl

@app.route("/")
def home():
    return "Hello, Flask!"

@app.route("/baidu")
def baidu():
    r = requests.get("http://www.baidu.com")
    r.encoding = "utf-8"
    return r.text

@app.route("/callback", methods=['GET', 'POST'])
def callback():
    code = request.args.get("code")
    params = {'client_id': '71f0d4c5afaac26bc07e',
        'client_secret': '006e2a3e9233f77bee26a1ebee987e556b6000ba',
        'code': code,
        'state': '1'}
    res = requests.post("https://github.com/login/oauth/access_token", data=params)
    # access_token=f90f9927d866ceea38dc30a18509a37bb3cc12a9&scope=user&token_type=bearer
    token = furl(url_for("callback") + "?" + res.text).args["access_token"]
    access_user_url = "https://api.github.com/user"
    r = requests.get(access_user_url, headers={'Authorization': 'token ' + token})
    return r.text

@app.route("/github")
def github():
    url = "https://github.com/login/oauth/authorize"
    params = {'client_id': '71f0d4c5afaac26bc07e',
        'redirect_uri': 'http://localhost:5000/callback',
        'scope': 'user',
        'state': '1'}
    url = furl(url).set(params)
    return redirect(url)

# http://localhost:5000/callback?code=9411a8c3b7ad809def3e&state=1
